//
//  CellOfProgram.m
//  To Feel
//
//  Created by 于成哲 on 16/7/23.
//  Copyright © 2016年 caoyu. All rights reserved.
//

#import "CellOfProgram.h"
#import "ModelOfProgram.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
@interface CellOfProgram ()
@property (nonatomic, retain) UIImageView *imagePic;

@property (nonatomic, retain) UILabel *labelOfTitle;

@property (nonatomic, retain) UILabel *labelOfPlayTimes;

@property (nonatomic, retain) UILabel *labelOfTime;

@property (nonatomic, retain) UILabel *labelOfDate;

@property (nonatomic, retain) UIImageView *imagepic;

@property (nonatomic, retain) UIImageView *imagepIc;
@end

@implementation CellOfProgram
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self createSubViews];
    }
    
    return self;
}

- (void)createSubViews {
    
    self.imagePic = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imagePic];
    self.imagePic.layer.masksToBounds = YES;
    self.imagePic.layer.cornerRadius = 25;
    self.imagePic.clipsToBounds = TRUE;
    
    self.labelOfTitle = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfTitle];
    self.labelOfTitle.numberOfLines = 0;
    
    self.labelOfDate = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfDate];
    self.labelOfDate.font = [UIFont systemFontOfSize:12];
    
    self.imagepic = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imagepic];
    
    self.labelOfPlayTimes = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfPlayTimes];
    self.labelOfPlayTimes.font = [UIFont systemFontOfSize:12];
    
    self.imagepIc = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imagepIc];
    
    self.labelOfTime = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfTime];
    self.labelOfTime.font = [UIFont systemFontOfSize:12];
    
//    self.buttonOfDownLoad = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonOfDownLoad];
    self.buttonOfDownLoad.layer.cornerRadius = 5;
    self.buttonOfDownLoad.layer.borderWidth = 1;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){0, 0, 0, 1});
    self.buttonOfDownLoad.layer.borderColor = borderColorRef;
    self.buttonOfDownLoad.titleLabel.textAlignment = NSTextAlignmentCenter;
}

- (void)setModel:(ModelOfProgram *)model {
    
    if (_model != model) {
        
        _model = model;
    }
    
    [self.imagePic sd_setImageWithURL:[NSURL URLWithString:model.coverMiddle]];
    
    self.labelOfTitle.text = model.title;
    
    self.labelOfPlayTimes.text = [NSString stringWithFormat:@"%ld", model.playtimes];
    
    NSInteger i = [model.title length];
    NSRange range = NSMakeRange(i - 8, 4);
    NSString *sub = [model.title substringWithRange:range];
    
    NSRange range1 = NSMakeRange(i - 4, 2);
    NSString *sub1 = [model.title substringWithRange:range1];
    
    self.labelOfDate.text = [NSString stringWithFormat:@"%@ - %@", sub, sub1];
    
    self.imagepic.image = [UIImage imageNamed:@"bofang.png"];
    
    self.imagepIc.image = [UIImage imageNamed:@"shijian.png"];
    
    NSString *str = [NSString stringWithFormat:@"%@", model.createdAt.description];
    NSMutableString *strOfTime = str.mutableCopy;
    [strOfTime deleteCharactersInRange:NSMakeRange(10, 3)];
    NSTimeInterval time = [strOfTime doubleValue];
    NSDate *detailDate=[NSDate dateWithTimeIntervalSince1970:time];
    NSMutableString *strOfDate = [NSMutableString stringWithFormat:@"%@", detailDate];
   [strOfDate deleteCharactersInRange:NSMakeRange(10, 15)];
    self.labelOfDate.text = strOfDate;
    NSLog(@"%@", strOfDate);
    
    NSMutableString *strOfdate = [NSMutableString stringWithFormat:@"%@", detailDate];
    [strOfdate deleteCharactersInRange:NSMakeRange(19, 6)];
    [strOfdate deleteCharactersInRange:NSMakeRange(0, 11)];
    self.labelOfTime.text = strOfdate;
    
    [self.buttonOfDownLoad setImage:[UIImage imageNamed:@"xiazai.png"] forState:UIControlStateNormal];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    [self.imagePic mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).with.offset(20);
        make.height.mas_equalTo(@50);
        make.left.equalTo(self.contentView).with.offset(20);
        make.width.mas_equalTo(@50);
    }];
    
    [self.labelOfTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).with.offset(20);
        make.left.equalTo(self.imagePic.mas_right).with.offset(15);
        make.height.mas_equalTo((CGRectGetHeight(self.contentView.bounds) - 20) / 2);
        make.right.equalTo(self.contentView).with.offset(-110);
    }];

    [self.labelOfDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).with.offset(20);
        make.right.equalTo(self.contentView).with.offset(-10);
        make.left.equalTo(self.labelOfTitle.mas_right).with.offset(30);
        make.height.mas_equalTo(@20);
    }];

    [self.imagepic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imagePic.mas_right).with.offset(15);
        make.height.mas_equalTo(@16);
        make.width.mas_equalTo(@16);
        make.bottom.equalTo(self.contentView).with.offset(-20);
    }];

    [self.labelOfPlayTimes mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView).with.offset(-20);
        make.left.equalTo(self.imagepic.mas_right).with.offset(4);
        make.height.mas_equalTo(@16);
        make.width.mas_equalTo(@60);
    }];

    [self.imagepIc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView).with.offset(-20);
        make.left.equalTo(self.labelOfPlayTimes.mas_right).with.offset(20);
        make.height.mas_equalTo(@16);
        make.width.mas_equalTo(@16);
    }];

    [self.labelOfTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imagepIc.mas_right).with.offset(4);
        make.bottom.equalTo(self.contentView).with.offset(-20);
        make.width.mas_equalTo(@120);
        make.height.mas_equalTo(@16);
    }];
    
    [self.buttonOfDownLoad mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView).with.offset(-20);
        make.right.equalTo(self.contentView).with.offset(-10);
        make.height.mas_equalTo(@32);
        make.width.mas_equalTo(@32);
    }];
}
@end
